{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 量子线路编译\n",
    "\n",
    "在 MindQuantum 中，量子线路编译由各种编译规则构成，这些规则会作用到量子线路对应的 DAG 图（Directed Acyclic Graph， 有向无环图）上，通过临近门融合、门替换的手段完成编译过程。\n",
    "\n",
    "## DAG 图\n",
    "\n",
    "在 MindQuantum 中，我们通过如下代码将一段量子线路转化为 DAG 图："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f56880dd580>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import DAGCircuit\n",
    "from mindquantum.core.circuit import Circuit\n",
    "\n",
    "circ = Circuit().h(0).x(1, 0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mindquantum.algorithm.compiler.dag.dag.DAGCircuit at 0x7f55fa76f190>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dag_circ = DAGCircuit(circ)\n",
    "dag_circ"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在 DAG 图中，我们选择量子比特为头部节点，每个节点都有其子节点和父节点，例如上述量子线路中，`q0` 节点的子节点时 `H`，而 `H` 的父节点为 `q0` 且子节点为 `X(1 <-: 0)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All head node:  {0: q0, 1: q1}\n",
      "Child node of q0:  {0: H(0)}\n",
      "Father node of H(0):  {0: q0}\n",
      "Child node of H(0):  {0: X(1 <-: 0)}\n"
     ]
    }
   ],
   "source": [
    "print(\"All head node: \", dag_circ.head_node)\n",
    "print(\"Child node of q0: \", dag_circ.head_node[0].child)\n",
    "print(\"Father node of H(0): \", dag_circ.head_node[0].child[0].father)\n",
    "print(\"Child node of H(0): \", dag_circ.head_node[0].child[0].child)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过上述的链式选择，我们可以选取到量子线路中的任意一个量子门。此外，DAG 图中的节点还有一个重要属性，`local` 指标，该指标表面节点是作用在哪些比特上，例如对于图中的 CNOT 门："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X(1 <-: 0)\n",
      "[1, 0]\n"
     ]
    }
   ],
   "source": [
    "cnot_node = dag_circ.head_node[0].child[0].child[0]\n",
    "print(cnot_node)\n",
    "print(cnot_node.local)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外，我们还可以直接将 DAG 复原为量子线路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"140.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"216.8\" height=\"140.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f55fa6ec9d0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dag_circ.to_circuit().svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有了这些基本的 DAG 图结构，我们便可以在其上做量子编译。下面，我们介绍各种编译规则。\n",
    "\n",
    "## 基本门分解规则 BasicDecompose\n",
    "\n",
    "基本门分解规则会将多控制位的量子门、自定义的量子门分解为一些基本量子门，这些基本量子门集合为: `X`, `Y`, `Z`, `H`, `S`, `Sdag`, `T`, `TDag`, `SWAP`, `CNOT`, `RX`, `RY`, `RZ`, `U3`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"188.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"216.8\" height=\"188.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"196.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<circle cx=\"92.8\" cy=\"100.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "X\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#fac209\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RX\n",
       " </text>\n",
       "<text x=\"152.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "a\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f5684308bb0>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import BasicDecompose\n",
    "from mindquantum.core.gates import RX\n",
    "circ = Circuit().x(2, [0, 1]) + RX('a').on(1, 0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1196.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"1196.8\" height=\"200.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"1176.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"1176.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"1176.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"192.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "<text x=\"224.8\" y=\"150.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "<circle cx=\"272.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"258.8\" x2=\"286.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"312.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "<circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"372.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"378.8\" x2=\"406.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"392.8\" x2=\"392.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"432.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"452.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "<text x=\"464.8\" y=\"150.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "<circle cx=\"512.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"512.8\" x2=\"512.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"492.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"498.8\" x2=\"526.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"512.8\" x2=\"512.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"552.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"572.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"612.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"632.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "<circle cx=\"572.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"572.8\" x2=\"572.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"552.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"558.8\" x2=\"586.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"572.8\" x2=\"572.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"612.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"632.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "<text x=\"644.8\" y=\"30.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"612.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"632.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "<circle cx=\"692.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"692.8\" x2=\"692.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"672.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"678.8\" x2=\"706.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"692.8\" x2=\"692.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"732.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"752.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "S\n",
       " </text>\n",
       "\n",
       "<circle cx=\"812.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"812.8\" x2=\"812.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"792.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"798.8\" x2=\"826.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"812.8\" x2=\"812.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"852.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"892.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"892.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "-1/2*a\n",
       " </text>\n",
       "\n",
       "<circle cx=\"972.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"972.8\" x2=\"972.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"952.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"958.8\" x2=\"986.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"972.8\" x2=\"972.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"1012.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"1052.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"1052.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "1/2*a\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"1112.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"1132.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "S\n",
       " </text>\n",
       "<text x=\"1144.8\" y=\"90.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f55fa752c70>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dag_circ = DAGCircuit(circ)\n",
    "rule = BasicDecompose()\n",
    "rule.do(dag_circ)\n",
    "new_circ = dag_circ.to_circuit()\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在 MindQuantum 中，我们调用编译规则的 `do` 函数来处理 DAG 图。至此，我们便完成了最基本的量子门分解。如上，我们还可发现，MindQuantum 支持对含参数门进行门分解。此外，MindQuantum 中的编译规则还支持调试功能，能够让你一步一步的看到哪些量子门发生了编译。我们通过调用编译规则的 `set_log_level` 来控制 log 信息，可选的 log_level 为：\n",
    "\n",
    "- 0： 不打印任何调试信息，为默认值。\n",
    "- 1： 打印粗略的调试信息\n",
    "- 2： 打印全部的调试信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Running \u001b[1;31mBasicDecompose\u001b[00m.\n",
      "  \u001b[1;31mBasicDecompose\u001b[00m: gate \u001b[1;34mX(2 <-: 0 1)\u001b[00m will be compiled.\n",
      "    - Running \u001b[1;31mBasicDecompose\u001b[00m.\n",
      "    - \u001b[1;31mBasicDecompose\u001b[00m: nothing happened.\n",
      "  \u001b[1;31mBasicDecompose\u001b[00m: gate \u001b[1;34mRX(a|1 <-: 0)\u001b[00m will be compiled.\n",
      "    - Running \u001b[1;31mBasicDecompose\u001b[00m.\n",
      "    - \u001b[1;31mBasicDecompose\u001b[00m: nothing happened.\n",
      "- \u001b[1;31mBasicDecompose\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1196.8\" height=\"200.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"0.0\" width=\"1196.8\" height=\"200.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"1176.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"1176.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"1176.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"192.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "<text x=\"224.8\" y=\"150.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "<circle cx=\"272.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"258.8\" x2=\"286.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"312.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "<circle cx=\"392.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"372.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"378.8\" x2=\"406.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"392.8\" x2=\"392.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"432.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"452.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "<text x=\"464.8\" y=\"150.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "<circle cx=\"512.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"512.8\" x2=\"512.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"492.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"498.8\" x2=\"526.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"512.8\" x2=\"512.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"552.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"572.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"612.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"632.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "<circle cx=\"572.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"572.8\" x2=\"572.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"552.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"558.8\" x2=\"586.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"572.8\" x2=\"572.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"612.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"632.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "<text x=\"644.8\" y=\"30.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"612.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"632.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "T\n",
       " </text>\n",
       "\n",
       "<circle cx=\"692.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"692.8\" x2=\"692.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"672.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"678.8\" x2=\"706.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"692.8\" x2=\"692.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"732.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"752.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "S\n",
       " </text>\n",
       "\n",
       "<circle cx=\"812.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"812.8\" x2=\"812.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"792.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"798.8\" x2=\"826.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"812.8\" x2=\"812.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"852.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"892.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"892.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "-1/2*a\n",
       " </text>\n",
       "\n",
       "<circle cx=\"972.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"972.8\" x2=\"972.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"952.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"958.8\" x2=\"986.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"972.8\" x2=\"972.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"1012.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n",
       "<text x=\"1052.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "RY\n",
       " </text>\n",
       "<text x=\"1052.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "1/2*a\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"1112.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"1132.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "S\n",
       " </text>\n",
       "<text x=\"1144.8\" y=\"90.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "†\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f55fa6ec310>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dag_circ = DAGCircuit(circ)\n",
    "rule = BasicDecompose()\n",
    "rule.set_log_level(2)\n",
    "rule.do(dag_circ)\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过打印出的调试信息，我们发现，`BasicDecompose` 对 `X(2 <-: 0 1)` 和 `RX(a|1 <-: 0)` 都做了门分解。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GateReplacer\n",
    "\n",
    "该编译规则能够将指定模式的量子门替换成想要的量子线路。\n",
    "\n",
    "例如对于如下的量子线路，我们可以将其中的 `CNOT` 全部换成 `H`、`CZ`和`H`的组合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"176.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"276.8\" height=\"176.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"78.8\" x2=\"106.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"92.8\" x2=\"92.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"198.8\" x2=\"226.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f55fa72db50>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = Circuit().x(1, 0).h(1).x(1, 2)\n",
    "circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为此，我们构建如下的编译规则："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GateReplacer<\n",
       "  q0: ──●──\n",
       "        │\n",
       "  q1: ──X── ->\n",
       "  q0: ───────●───────\n",
       "             │\n",
       "  q1: ──H────Z────H──\n",
       ">"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import GateReplacer\n",
    "from mindquantum.core.gates import X\n",
    "rule = GateReplacer(X.on(1, 0), Circuit().h(1).z(1, 0).h(1))\n",
    "rule.set_log_level(2)\n",
    "rule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Running \u001b[1;31mGateReplacer\u001b[00m.\n",
      "  \u001b[1;31mGateReplacer\u001b[00m: gate \u001b[1;34mX(1 <-: 2)\u001b[00m will be replaced.\n",
      "  \u001b[1;31mGateReplacer\u001b[00m: gate \u001b[1;34mX(1 <-: 0)\u001b[00m will be replaced.\n",
      "- \u001b[1;31mGateReplacer\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"176.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"516.8\" height=\"176.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"392.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"392.8\" x2=\"392.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"372.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"392.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"432.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"452.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f55fa72dc40>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dag_circ = DAGCircuit(circ)\n",
    "rule.do(dag_circ)\n",
    "new_circ = dag_circ.to_circuit()\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由上面结果可知，除了第一个 CNOT 门，第二个作用顺序不一样的 CNOT 门我们也替换掉了。根据 `GateReplacer` 规则，我们在 MindQuantum 中实现了内置的将 CNOT 线路和 CZ 线路相互转化的编译维泽：\n",
    "\n",
    "- `CXToCZ`：将 CNOT 门替换为 CZ 门。\n",
    "- `CZToCX`：将 CZ 门替换成 CNOT 门。\n",
    "\n",
    "根据上面的结果，我们发现，编译后产生了冗余的 H 门，这些 H 门可以删除掉，因此我们也实现了一个将一些量子门进行融合的编译规则，例如将两个 X 门融合为一个 identity，将两个 RX 门融合为一个 RX 门。\n",
    "\n",
    "## FullyNeighborCanceler\n",
    "\n",
    "该编译规则可以尝试将临近的多个门融合为一个量子门，或者直接删除掉（如果互为厄米共轭）。对于上述量子线路，我们接着利用该编译规则进行编译："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Running \u001b[1;31mFullyNeighborCanceler\u001b[00m: 1 child (\u001b[1;33mSimpleNeighborCanceler\u001b[00m, ).\n",
      "  - Running \u001b[1;31mSimpleNeighborCanceler\u001b[00m.\n",
      "      \u001b[1;31mSimpleNeighborCanceler\u001b[00m: merge \u001b[1;34mH(1)\u001b[00m and \u001b[1;34mH(1)\u001b[00m.\n",
      "  - \u001b[1;31mSimpleNeighborCanceler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "  \u001b[1;31mFullyNeighborCanceler\u001b[00m: state for each rule -> [\u001b[1;35mTrue\u001b[00m]\n",
      "  - Running \u001b[1;31mSimpleNeighborCanceler\u001b[00m.\n",
      "  - \u001b[1;31mSimpleNeighborCanceler\u001b[00m: nothing happened.\n",
      "  \u001b[1;31mFullyNeighborCanceler\u001b[00m: state for each rule -> [False]\n",
      "- \u001b[1;31mFullyNeighborCanceler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"396.8\" height=\"176.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"396.8\" height=\"176.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f56880d6580>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import FullyNeighborCanceler\n",
    "\n",
    "rule = FullyNeighborCanceler()\n",
    "rule.set_all_log_level(2)\n",
    "rule.do(dag_circ)\n",
    "new_circ = dag_circ.to_circuit()\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由此，我们将对于的 H 门删除掉了。\n",
    "\n",
    "在上述编译中，我们依次调用了两个编译规则：`GateReplacer` 和 `FullyNeighborCanceler`，为了更好将这些编译规则组织起来，我们实现了两个编译规则的容器，将多个编译规则组合成一个整体。\n",
    "\n",
    "## SequentialCompiler\n",
    "\n",
    "`SequentialCompiler` 是一个序列执行子编译规则的容器，其适用方法如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SequentialCompiler<\n",
       "  CXToCZ<\n",
       "    GateReplacer<\n",
       "      q0: ──X──\n",
       "            │\n",
       "      q1: ──●── ->\n",
       "      q0: ──H────Z────H──\n",
       "                 │\n",
       "      q1: ───────●───────\n",
       "    >\n",
       "    GateReplacer<\n",
       "      q0: ──X──\n",
       "            │\n",
       "      q1: ──●── ->\n",
       "      q0: ──H────Z────H──\n",
       "                 │\n",
       "      q1: ───────●───────\n",
       "    >\n",
       "  >\n",
       "  FullyNeighborCanceler<\n",
       "    SimpleNeighborCanceler<>\n",
       "  >\n",
       ">"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import SequentialCompiler, CXToCZ\n",
    "\n",
    "\n",
    "\n",
    "rules = SequentialCompiler([\n",
    "    CXToCZ(),\n",
    "    FullyNeighborCanceler(),\n",
    "])\n",
    "rules.set_all_log_level(2)\n",
    "rules"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由上我们可知，rules 包含两条编译规则，`CXToCZ` 和 `FullyNeighborCanceler`。注意，我们内置的 `CXToCZ` 是一个能够将 MindQuantum 中的 `X.on(1, 0)` 和 `CNOT.on([1, 0])` 都转化为 CZ 线路的编译规则。\n",
    "\n",
    "我们试着编译如下线路："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"176.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"276.8\" height=\"176.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"256.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"78.8\" x2=\"106.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"92.8\" x2=\"92.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n",
       "<line x1=\"198.8\" x2=\"226.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "<line x1=\"212.8\" x2=\"212.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f5684308520>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = circ = Circuit().x(1, 0).h(1).x(1, 2)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Running \u001b[1;31mSequentialCompiler\u001b[00m: 2 child (\u001b[1;33mCXToCZ\u001b[00m, \u001b[1;33mFullyNeighborCanceler\u001b[00m, ).\n",
      "  - Running \u001b[1;31mCXToCZ\u001b[00m: 2 child (\u001b[1;33mGateReplacer\u001b[00m, \u001b[1;33mGateReplacer\u001b[00m, ).\n",
      "    - Running \u001b[1;31mGateReplacer\u001b[00m.\n",
      "      \u001b[1;31mGateReplacer\u001b[00m: gate \u001b[1;34mX(1 <-: 0)\u001b[00m will be replaced.\n",
      "      \u001b[1;31mGateReplacer\u001b[00m: gate \u001b[1;34mX(1 <-: 2)\u001b[00m will be replaced.\n",
      "    - \u001b[1;31mGateReplacer\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "    - Running \u001b[1;31mGateReplacer\u001b[00m.\n",
      "    - \u001b[1;31mGateReplacer\u001b[00m: nothing happened.\n",
      "    \u001b[1;31mCXToCZ\u001b[00m: state for each rule -> [\u001b[1;35mTrue\u001b[00m, False]\n",
      "  - \u001b[1;31mCXToCZ\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "  - Running \u001b[1;31mFullyNeighborCanceler\u001b[00m: 1 child (\u001b[1;33mSimpleNeighborCanceler\u001b[00m, ).\n",
      "    - Running \u001b[1;31mSimpleNeighborCanceler\u001b[00m.\n",
      "        \u001b[1;31mSimpleNeighborCanceler\u001b[00m: merge \u001b[1;34mH(1)\u001b[00m and \u001b[1;34mH(1)\u001b[00m.\n",
      "    - \u001b[1;31mSimpleNeighborCanceler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "    \u001b[1;31mFullyNeighborCanceler\u001b[00m: state for each rule -> [\u001b[1;35mTrue\u001b[00m]\n",
      "    - Running \u001b[1;31mSimpleNeighborCanceler\u001b[00m.\n",
      "    - \u001b[1;31mSimpleNeighborCanceler\u001b[00m: nothing happened.\n",
      "    \u001b[1;31mFullyNeighborCanceler\u001b[00m: state for each rule -> [False]\n",
      "  - \u001b[1;31mFullyNeighborCanceler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "  \u001b[1;31mSequentialCompiler\u001b[00m: state for each rule -> [\u001b[1;35mTrue\u001b[00m, \u001b[1;35mTrue\u001b[00m]\n",
      "- \u001b[1;31mSequentialCompiler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"396.8\" height=\"176.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"396.8\" height=\"176.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f55fa73a190>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dag_circ = DAGCircuit(circ)\n",
    "rules.do(dag_circ)\n",
    "new_circ = dag_circ.to_circuit()\n",
    "new_circ.svg()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## KroneckerSeqCompiler\n",
    "\n",
    "`KroneckerSeqCompiler` 也是一个由多个编译规则构成的容器，但是与 `SequentialCompiler` 不同的时，`KroneckerSeqCompiler`会重复执行内部的子编译规则，直到编译规则无法对量子线路产生效果为止。\n",
    "\n",
    "## 总结\n",
    "\n",
    "如上，便是 MindQuantum 中的量子线路编译实现。基于这些规则，我们也内置实现了一些适用于不同量子芯片编译规则，如只支持 CZ 门而不支持 CNOT 门的芯片，我们可以使用 `CZBasedChipCompiler` 来进行编译。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CZBasedChipCompiler<\n",
       "  BasicDecompose<>\n",
       "  CXToCZ<\n",
       "    GateReplacer<\n",
       "      q0: ──X──\n",
       "            │\n",
       "      q1: ──●── ->\n",
       "      q0: ──H────Z────H──\n",
       "                 │\n",
       "      q1: ───────●───────\n",
       "    >\n",
       "    GateReplacer<\n",
       "      q0: ──X──\n",
       "            │\n",
       "      q1: ──●── ->\n",
       "      q0: ──H────Z────H──\n",
       "                 │\n",
       "      q1: ───────●───────\n",
       "    >\n",
       "  >\n",
       "  FullyNeighborCanceler<\n",
       "    SimpleNeighborCanceler<>\n",
       "  >\n",
       ">"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import CZBasedChipCompiler\n",
    "\n",
    "CZBasedChipCompiler()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从结果我们可以发现，`CZBasedChipCompiler` 由三条编译规则构成：基本量子门分解规则，CX转化为CZ的编译规则，临近门消除规则。\n",
    "\n",
    "## 附录\n",
    "\n",
    "为了方便的使用这些编译规则进行量子线路编译，我们把编译规则的执行打包成了一个函数 `compile_circuit`，可以直接将给定的量子线路按照制定的规则进行编译。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Running \u001b[1;31mCZBasedChipCompiler\u001b[00m: 3 child (\u001b[1;33mBasicDecompose\u001b[00m, \u001b[1;33mCXToCZ\u001b[00m, \u001b[1;33mFullyNeighborCanceler\u001b[00m, ).\n",
      "  - Running \u001b[1;31mBasicDecompose\u001b[00m.\n",
      "  - \u001b[1;31mBasicDecompose\u001b[00m: nothing happened.\n",
      "  - Running \u001b[1;31mCXToCZ\u001b[00m: 2 child (\u001b[1;33mGateReplacer\u001b[00m, \u001b[1;33mGateReplacer\u001b[00m, ).\n",
      "    - Running \u001b[1;31mGateReplacer\u001b[00m.\n",
      "      \u001b[1;31mGateReplacer\u001b[00m: gate \u001b[1;34mX(1 <-: 0)\u001b[00m will be replaced.\n",
      "      \u001b[1;31mGateReplacer\u001b[00m: gate \u001b[1;34mX(1 <-: 2)\u001b[00m will be replaced.\n",
      "    - \u001b[1;31mGateReplacer\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "    - Running \u001b[1;31mGateReplacer\u001b[00m.\n",
      "    - \u001b[1;31mGateReplacer\u001b[00m: nothing happened.\n",
      "    \u001b[1;31mCXToCZ\u001b[00m: state for each rule -> [\u001b[1;35mTrue\u001b[00m, False]\n",
      "  - \u001b[1;31mCXToCZ\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "  - Running \u001b[1;31mFullyNeighborCanceler\u001b[00m: 1 child (\u001b[1;33mSimpleNeighborCanceler\u001b[00m, ).\n",
      "    - Running \u001b[1;31mSimpleNeighborCanceler\u001b[00m.\n",
      "        \u001b[1;31mSimpleNeighborCanceler\u001b[00m: merge \u001b[1;34mH(1)\u001b[00m and \u001b[1;34mH(1)\u001b[00m.\n",
      "    - \u001b[1;31mSimpleNeighborCanceler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "    \u001b[1;31mFullyNeighborCanceler\u001b[00m: state for each rule -> [\u001b[1;35mTrue\u001b[00m]\n",
      "    - Running \u001b[1;31mSimpleNeighborCanceler\u001b[00m.\n",
      "    - \u001b[1;31mSimpleNeighborCanceler\u001b[00m: nothing happened.\n",
      "    \u001b[1;31mFullyNeighborCanceler\u001b[00m: state for each rule -> [False]\n",
      "  - \u001b[1;31mFullyNeighborCanceler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n",
      "  \u001b[1;31mCZBasedChipCompiler\u001b[00m: state for each rule -> [False, \u001b[1;35mTrue\u001b[00m, \u001b[1;35mTrue\u001b[00m]\n",
      "- \u001b[1;31mCZBasedChipCompiler\u001b[00m: \u001b[1;35msuccessfully compiled\u001b[00m.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"396.8\" height=\"176.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<rect x=\"0\" y=\"12.0\" width=\"396.8\" height=\"176.0\" fill=\"#ffffff\" />\n",
       "<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q0:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q1:\n",
       " </text>\n",
       "<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\n",
       "q2:\n",
       " </text>\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "<line x1=\"48.8\" x2=\"376.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n",
       "\n",
       "<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"152.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n",
       "<line x1=\"272.8\" x2=\"272.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n",
       "<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "Z\n",
       " </text>\n",
       "\n",
       "\n",
       "<rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n",
       "<text x=\"332.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n",
       "H\n",
       " </text>\n",
       "\n",
       "</svg></div>"
      ],
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f56843088e0>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.compiler import compile_circuit\n",
    "\n",
    "new_circ = compile_circuit(CZBasedChipCompiler().set_all_log_level(2), circ)\n",
    "new_circ.svg()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
